package com.swjd.servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.swjd.bean.register;

public class registersvlt extends HttpServlet {

	public registersvlt() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	boolean flag;
	public void doGet(HttpServletRequest req, HttpServletResponse res)
	    throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8"); 	
	    //String userid =req.getParameter("userid");
	    int success = 0;
	    String action = req.getParameter("action");
	    register u = null;
	    String message="";

	    if ("register".equalsIgnoreCase(action)) {
	    	flag=true;	
	     	u = doNew(req,res);
	     	if(flag)doError(req,res,"注册成功！");
	     	
	     	    
	    }
				   
	    /* if ("delete".equalsIgnoreCase(action)) {
	    	try{			
	      	success = doDelete(userid);	
	      	    }
	      	    catch(SQLException e){}				
	    	if (success != 1) {
	    		doError(req, res, "usersvlt: Delete unsuccessful. Rows affected: " + success);
	    	} else {
	    		res.sendRedirect("userinfo.jsp");
	    	}
	   
	    }*/
	    }
	    

	 public register doNew(HttpServletRequest req,HttpServletResponse res )
	                           throws ServletException,IOException{
		  register r= new register();                     	
	      String userid=req.getParameter("userid");
	      //System.out.print(userid);
	      
	      String username=req.getParameter("username");
	      //System.out.print(userid);
	      
	      String sex=req.getParameter("sex");
	      String id=req.getParameter("id");
	      String password=req.getParameter("password");
	      String email=req.getParameter("email");
	      String phone = req.getParameter("phone");
	      String address=req.getParameter("address");
	      
	      
	      
	      if(hasLogin(req,res,userid)){
	      
	      r.setuserId(userid);
	      r.setUserName(username);
	      r.setSex(sex);
	      r.setId(id);
	      
	      r.setPassword(password);
	      r.setEmail(email);
	      r.setPhone(phone);
	      r.setAddress(address);
	      
	      
	      r.addUser(); }  
	      return r;                	
	                           	
	                           	}


		
	public void sendBean(HttpServletRequest req, HttpServletResponse res,
			register r, String target)
	                       throws ServletException, IOException {
	    req.setAttribute("register", r);
	    RequestDispatcher rd = getServletContext().getRequestDispatcher(target);
	    rd.forward(req, res);
	  }
	  
	  
	  public void doError(HttpServletRequest req,
	                      HttpServletResponse res,
	                      String str)
	                      throws ServletException, IOException {

		 flag=false;
	    req.setAttribute("problem", str);
	    RequestDispatcher rd = getServletContext().getRequestDispatcher("/errorpage.jsp");
	    rd.forward(req, res);
	  }
	  
	  public boolean hasLogin(HttpServletRequest req, HttpServletResponse res,String userid)
	  throws ServletException, IOException{
	  	boolean f=true;
	  	String message="对不起，该用户已经被注册过了!";
	  	register u= new register();
	  	f= u.hasLogin(userid);
	  	if(f==false){
	  	doError(req,res,message);	
	  		}
	  		return f;
	  	}
	  
	 /* public boolean isTrue(HttpServletRequest req, HttpServletResponse res,
	                        String name,String password,String id)
	                        throws ServletException, IOException {
	   boolean f=true;                     	
	   String message ="";
	   
	   if(name==null || name.equals(""))  {
	   	f=false;
	   	message="姓名不能为空，请重新填写！";
	   	if(flag)doError(req,res,message);	}
	  	
	       
	   if(password==null || password.equals(""))  {
	   	f=false;
	   	message="密码不能为空，请重新填写！";
	   	if(flag) doError(req,res,message);	}  
       
	   if(id==null || id.equals(""))  {
		   	f=false;
		   	message="身份证号不能为空，请重新填写！";
		   	if(flag) doError(req,res,message);	} 
	     return f;
	     
	  }*/
	  
	  public void doPost(HttpServletRequest req, HttpServletResponse res)
	    throws ServletException, IOException {

	    doGet(req, res);

	  }

}
